package com.flashcash.solu.utils;

import android.text.TextUtils;

import com.x.leo.apphelper.log.XLog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by Miaoke on 2017/3/23.
 */

public class TimeFormat {

    /**
     * 转化成英文时间
     *
     * @param srcTime
     * @return
     */
    public static String convertEnglishTime(String srcTime) {
//        return srcTime;
//        return convertEnglishTime(srcTime, "MM dd,yyyy");
        return convertTime(srcTime, "yyyy-MM-dd HH:mm");
    }

    public static String convertEnglishTime(String srcTime, String formatStyle) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
        SimpleDateFormat simpleDateFormatLocal = new SimpleDateFormat(formatStyle);
        simpleDateFormatLocal.setTimeZone(TimeZone.getDefault());
        try {
            if (TextUtils.isEmpty(srcTime)) {
                return "0";
            }
            Date srcDate = simpleDateFormat.parse(srcTime);
            String targetTime = simpleDateFormatLocal.format(srcDate);
            return targetTime;
        } catch (ParseException e) {
            e.printStackTrace();
            XLog.INSTANCE.e("parse time exception: ", e, 100);
        }

        return "0";
    }

    public static String convertTime(String srcTime) {
        return convertTime(srcTime, "yyyy-MM-dd HH:mm");
    }

    public static String convertTimeDay(String srcTime) {
        return convertTime(srcTime, "yy-MM-dd");
    }

    public static String convertTime(String srcTime, String formatStyle) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
        SimpleDateFormat simpleDateFormatLocal = new SimpleDateFormat(formatStyle);
        simpleDateFormatLocal.setTimeZone(TimeZone.getDefault());
        try {
            if (TextUtils.isEmpty(srcTime)) {
                return "0";
            }
            Date srcDate = simpleDateFormat.parse(srcTime);
            String targetTime = simpleDateFormatLocal.format(srcDate);
            return targetTime;
        } catch (ParseException e) {
            e.printStackTrace();
            XLog.INSTANCE.e("parse time exception: ", e, 100);
        }

        return "0";
    }

    public static long obtainLongTime(String srcTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
        try {
            if (TextUtils.isEmpty(srcTime)) {
                return 0L;
            }
            Date srcDate = simpleDateFormat.parse(srcTime);
            if (srcDate == null) {
                return 0L;
            }
            return srcDate.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            XLog.INSTANCE.e("parse time exception: ", e, 100);
        }

        return 0;
    }
}
